package com.jhy.lads.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author jihaiyu
 * @version V1.0
 * @Description: DateUtil工具类
 * @date 2017年7月10日
 */
public class DateUtil {

    public static final String PATTERN_DATE_DEFAULT = "yyyy-MM-dd";

    public static final String PATTERN_DATE_ACTIVITY = "yyyy/MM/dd HH:mm:ss";

    public static final String PATTERN_DATE_RISK = "yyyy-MM-dd HH:mm:ss";

    public static final String PATTERN_DATE_ACTIVITY_DEFAULT = "yyyy/MM/dd";

    public static final String PATTERN_DATE_OFFSETCODE = "yyyyMMdd";

    /**
     * @param pattern
     * @param strDate
     * @return Date
     * @throws ParseException
     * @see java.text.SimpleDateFormat
     */
    public static final Date convertStringToDate(String pattern, String strDate) {
        SimpleDateFormat df = null;
        Date date = null;
        if(pattern == null) {
            df = new SimpleDateFormat(PATTERN_DATE_RISK);
        } else {
            df = new SimpleDateFormat(pattern);
        }

        try {
            date = df.parse(strDate);
        } catch(ParseException pe) {
            pe.printStackTrace();
        }

        return date;
    }


    /**
     * @param pattern
     * @param date
     * @return
     */
    public static final String convertDateToString(String pattern, Date date) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if(date == null) {
        } else {
            df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }

        return returnValue;
    }

    /**
     * @param date
     * @param date
     * @return
     */
    public static final String convertDateToString(Date date) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if(date == null) {
        } else {
            df = new SimpleDateFormat(PATTERN_DATE_DEFAULT);
            returnValue = df.format(date);
        }

        return returnValue;
    }

    /**
     * @return
     */
    public static String currentDateStr() {
        Calendar calendar = Calendar.getInstance();
        return convertDateToString(PATTERN_DATE_DEFAULT, calendar.getTime());
    }

    /**
     * 获取几天前的日期字符串
     *
     * @param days
     * @return
     */
    public static String dateStrBefore(int days) {
        return convertDateToString(dateBefore(days));
    }

    /**
     * 获取几天前的日期字符串
     *
     * @param days
     * @return
     */
    public static String dateStrBefore(Date date, int days) {
        return convertDateToString(dateBefore(date, days));
    }

    /**
     * 获取几天前的日期
     *
     * @param days
     * @return
     */
    public static Date dateBefore(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -days);
        return calendar.getTime();
    }

    /**
     * 获取几天前的日期
     *
     * @param days
     * @return
     */
    public static Date dateBefore(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -days);
        return calendar.getTime();
    }

    /**
     * 获取几天后的日期
     *
     * @param days
     * @return
     */
    public static Date dateAfter(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 获取几小时后的日期
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date dateAfterHour(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hours);
        return calendar.getTime();
    }

    /**
     * Get the exact start date of a day
     *
     * @param d
     * @return
     */
    public static Date getDayStartDate(Date d) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            long between_days = (time2 - time1) / (1000 * 3600 * 24);
            return Integer.parseInt(String.valueOf(between_days));
        } catch(Exception e) {
            return 0;
        }
    }

    /**
     * 获取系统当前SQL类型的Date
     *
     * @return 当前时间
     */
    public static java.sql.Date getSqlDate() {
        return new java.sql.Date(System.currentTimeMillis());
    }

    public static String getBatchCode() {
        return convertDateToString(DateUtil.PATTERN_DATE_OFFSETCODE, new Date());
    }

    public static boolean isEqualDay(Date date1, Date date2) {
        int i = daysBetween(date1, date2);
        if(i == 0) {
            return true;
        }
        return false;
    }

    public static Date minuteBefor(Date nowdate, int befor) {
        long dateStamp = nowdate.getTime() - befor * 60 * 1000;
        return new Date(dateStamp);
    }

    public static Date getRedeemConfirmDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            System.out.println(true);
        }else{
            System.out.println(false);
        }
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    public static String getSundayOfThisWeek(int week) {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = week;
        c.add(Calendar.DATE, -day_of_week + week);
        return convertDateToString(c.getTime());
    }

    // 获得下周星期一的日期
    public static Date getNextMonday(Date gmtCreate) {
        int mondayPlus = getMondayPlus(gmtCreate);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
        Date monday = currentDate.getTime();
        return monday;
    }


    // 获得当前日期与本周日相差的天数
    private static int getMondayPlus(Date gmtCreate) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(gmtCreate);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return 1 - dayOfWeek;
        }
    }

    public static void main(String args[]) {
        //String str = convertDateToString(DateUtil.PATTERN_DATE_ACTIVITY, new Date());
        //System.out.print(dateBefore(new Date(),-1));
//        System.out.println(getSundayOfThisWeek(1));
//        System.out.println(getSundayOfThisWeek(2));
//        System.out.println(getSundayOfThisWeek(3));
//        System.out.println(getSundayOfThisWeek(4));
//        System.out.println(getSundayOfThisWeek(5));
//        System.out.println(getSundayOfThisWeek(6));
//        System.out.println(getSundayOfThisWeek(7));

        System.out.println(getRedeemConfirmDate(new Date()));
//        System.out.println(convertDateToString(getNextMonday(new Date())));
//        System.out.println(DateUtil.convertStringToDate(DateUtil.PATTERN_DATE_ACTIVITY, "2016/08/31 00:00:00").getTime());
//        System.out.println(new Date(1472572800000L));
    }

}
